/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZMessage;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import java.util.List;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestIndex
/*     */   extends TestCase
/*     */ {
/*  33 */   private static final String NAME_PREFIX = TestIndex.class.getSimpleName();
/*     */   private static final String USER_NAME = "user1";
/*     */   private int mOriginalTextLimit;
/*     */   
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/*  40 */     this.mOriginalTextLimit = Integer.parseInt(TestUtil.getServerAttr("zimbraAttachmentsIndexedTextLimit"));
/*  41 */     cleanUp();
/*     */   }
/*     */   
/*     */   public void testIndexedTextLimit()
/*     */     throws Exception
/*     */   {
/*  47 */     StringBuilder body = new StringBuilder();
/*  48 */     for (int i = 1; i < 100; i++) {
/*  49 */       body.append("Walrus walrus walrus walrus walrus walrus walrus.\n");
/*     */     }
/*  51 */     body.append("Goo goo goo joob.\n");
/*     */     
/*     */ 
/*  54 */     setTextLimit(50);
/*  55 */     String subject = NAME_PREFIX + " text attachment 1";
/*  56 */     String msgId = sendMessage(subject, body.toString().getBytes(), "attachment.txt", "text/plain").getId();
/*  57 */     checkQuery("in:inbox subject:\"" + subject + "\" walrus", msgId);
/*  58 */     checkQuery("in:inbox subject:\"" + subject + "\" joob", null);
/*     */     
/*     */ 
/*  61 */     subject = NAME_PREFIX + " HTML attachment 1";
/*  62 */     String htmlBody = "<html>\n" + body + "</html>";
/*  63 */     msgId = sendMessage(subject, htmlBody.getBytes(), "attachment.html", "text/html").getId();
/*  64 */     checkQuery("in:inbox subject:\"" + subject + "\" walrus", msgId);
/*  65 */     checkQuery("in:inbox subject:\"" + subject + "\" joob", null);
/*     */     
/*     */ 
/*  68 */     setTextLimit(100000);
/*  69 */     subject = NAME_PREFIX + " text attachment 2";
/*  70 */     msgId = sendMessage(subject, body.toString().getBytes(), "attachment.txt", "text/plain").getId();
/*  71 */     checkQuery("in:inbox subject:\"" + subject + "\" walrus", msgId);
/*  72 */     checkQuery("in:inbox subject:\"" + subject + "\" joob", msgId);
/*     */     
/*     */ 
/*  75 */     subject = NAME_PREFIX + " HTML attachment 2";
/*  76 */     msgId = sendMessage(subject, htmlBody.getBytes(), "attachment.html", "text/html").getId();
/*  77 */     checkQuery("in:inbox subject:\"" + subject + "\" walrus", msgId);
/*  78 */     checkQuery("in:inbox subject:\"" + subject + "\" joob", msgId);
/*     */     
/*     */ 
/*  81 */     subject = NAME_PREFIX + " subject";
/*  82 */     String attachedMsg = TestUtil.getTestMessage("Pigs from a gun", "recipient", "sender", null);
/*  83 */     setTextLimit(4);
/*  84 */     msgId = sendMessage(subject, attachedMsg.getBytes(), "attachment.msg", "message/rfc822").getId();
/*  85 */     checkQuery("in:inbox subject:\"" + subject + "\" pigs", msgId);
/*  86 */     checkQuery("in:inbox subject:\"" + subject + "\" gun", null);
/*     */   }
/*     */   
/*     */   public void testRemovingStopWords() throws Exception {
/*  90 */     Provisioning.getInstance().getConfig().removeDefaultAnalyzerStopWords("a");
/*  91 */     String body = "Walrus walrus walrus walrus walrus walrus walrus is a walrus.\n";
/*  92 */     String subject = NAME_PREFIX + " text with A letter";
/*  93 */     String msgId = sendMessage(subject, body.toString().getBytes(), "attachment.txt", "text/plain").getId();
/*     */     
/*     */ 
/*  96 */     checkQuery("in:inbox subject:\" a \"", msgId);
/*  97 */     checkQuery("in:inbox subject:\"" + subject + "\" A", msgId);
/*     */   }
/*     */   
/*     */   public void testExistingStopWords() throws Exception {
/* 101 */     Provisioning.getInstance().getConfig().removeDefaultAnalyzerStopWords("a");
/* 102 */     String body = "Walrus walrus walrus walrus walrus walrus walrus is a walrus.\n";
/* 103 */     String subject = NAME_PREFIX + " text with A letter";
/* 104 */     String msgId = sendMessage(subject, body.toString().getBytes(), "attachment.txt", "text/plain").getId();
/*     */     
/*     */ 
/* 107 */     checkQuery("in:inbox subject:\" is \"", null);
/*     */   }
/*     */   
/*     */ 
/*     */   public void testFilenameSearch()
/*     */     throws Exception
/*     */   {
/* 114 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 115 */     String filename = NAME_PREFIX + " testFilenameSearch.txt";
/* 116 */     TestUtil.createDocument(mbox, Integer.toString(16), filename, "text/plain", "This is the data for testFilenameSearch.".getBytes());
/*     */     
/* 118 */     assertEquals(0, TestUtil.search(mbox, "filename:Blob*", "document").size());
/* 119 */     assertEquals(1, TestUtil.search(mbox, "filename:\"" + filename + "\"", "document").size());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private ZMessage sendMessage(String subject, byte[] attData, String attName, String attContentType)
/*     */     throws Exception
/*     */   {
/* 136 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 137 */     String attachmentId = mbox.uploadAttachment(attName, attData, attContentType, 5000);
/* 138 */     TestUtil.sendMessage(mbox, "user1", subject, "Cranberry sauce", attachmentId);
/* 139 */     String query = "in:inbox subject:\"" + subject + "\"";
/* 140 */     return TestUtil.waitForMessage(mbox, query);
/*     */   }
/*     */   
/*     */   private void checkQuery(String query, String msgId) throws Exception
/*     */   {
/* 145 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 146 */     List<ZMessage> messages = TestUtil.search(mbox, query);
/* 147 */     if (msgId == null) {
/* 148 */       assertEquals(0, messages.size());
/*     */     } else {
/* 150 */       assertEquals(1, messages.size());
/* 151 */       assertEquals(msgId, ((ZMessage)messages.get(0)).getId());
/*     */     }
/*     */   }
/*     */   
/*     */   private void setTextLimit(int numBytes) throws Exception
/*     */   {
/* 157 */     TestUtil.setServerAttr("zimbraAttachmentsIndexedTextLimit", Integer.toString(numBytes));
/*     */   }
/*     */   
/*     */   public void tearDown()
/*     */     throws Exception
/*     */   {
/* 163 */     setTextLimit(this.mOriginalTextLimit);
/* 164 */     Provisioning.getInstance().getConfig().addDefaultAnalyzerStopWords("a");
/* 165 */     cleanUp();
/*     */   }
/*     */   
/*     */   private void cleanUp() throws Exception
/*     */   {
/* 170 */     TestUtil.deleteTestData("user1", NAME_PREFIX);
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception
/*     */   {
/* 175 */     TestUtil.cliSetup();
/* 176 */     TestUtil.runTest(TestIndex.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestIndex.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */